﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YUIAspNet
{
    public class BaseOverlayButton : Button
    {
        Panel _pnlOverlay;
        Panel _pnlInner;

        public BaseOverlayButton()
        {
            this.TypeName = "YUIAspNet.Button"; // force the type to be still a button...

            this.ButtonType = YUIAspNet.Common.ButtonType.Menu;

            this.Init += new EventHandler(BaseOverlayButton_Init);

            _pnlOverlay = new Panel();
            _pnlOverlay.ID = "menucontainer";
            _pnlOverlay.CssClass = "yui-module yui-overlay yuimenu yui-button-menu yui-menu-button-menu";
            _pnlOverlay.Style.Add("visibility", "hidden");
            _pnlOverlay.Style.Add("position", "absolute");

            _pnlInner = new Panel();
            _pnlInner.ID = "pnlInner";
            _pnlOverlay.Controls.Add(_pnlInner);
        }

        void BaseOverlayButton_Init(object sender, EventArgs e)
        {
            if (OverlayTemplate != null)
            {
                this.Controls.Add(_pnlOverlay);
                this.OverlayTemplate.InstantiateIn(_pnlInner);

                AddProperty("MenuContainer", _pnlOverlay.ClientID);
            }
        }

        public Unit OverlayWidth
        {
            get { return this._pnlOverlay.Width; }
            set { _pnlOverlay.Width = value; }
        }

        public Unit OverlayHeight
        {
            get { return this._pnlOverlay.Height; }
            set { _pnlOverlay.Height = value; }
        }

        protected override void RegisterClientInitialization()
        {
             //when overlay is defined, only split or menu can be defined!
            if (this.OverlayTemplate != null &&
                (this.ButtonType != YUIAspNet.Common.ButtonType.Menu) || (this.ButtonType == YUIAspNet.Common.ButtonType.Split))
                this.ButtonType = YUIAspNet.Common.ButtonType.Menu;

            base.RegisterClientInitialization();
        }

        /// <summary>
        /// Gets or sets the overlay ID that will replace the items.
        /// </summary>
        /// <value>The overlay ID.</value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate OverlayTemplate
        { get; set; }

        public override List<MenuButtonItem> Items
        {
            get
            {
                throw new Exception("Items are not supported for this control! Use either the Button or the OverlayTemplate property instead of this.");
            }
            set
            {
                throw new Exception("Items are not supported for this control! Use either the Button or the OverlayTemplate property instead of this.");
            }
        }
    }
}
